#include "../include/header/cat-config.hpp"

#include "../include/util/cmem.hpp"

#include "../include/cat-proc.hpp"
#include "../include/cat-window.hpp"


bool cat_open_window(cat_window_t* _cat_window, const char* title, const char* icon)
{
	cat_data_t* config = cat_load_default_config();
	if (!config)
		return false;

	int WIDTH, HEIGHT;
	WIDTH = config->json["main_decoration"]["window_size"][0].asInt();
	HEIGHT = config->json["main_decoration"]["window_size"][1].asInt();

	if(title)
		_cat_window->window->create(sf::VideoMode(WIDTH, HEIGHT), title, sf::Style::Decorated );
	else
		_cat_window->window->create(sf::VideoMode(WIDTH, HEIGHT), CAT_CORE_VERSION, sf::Style::Decorated);

	if (_cat_window->window->isOpen()) {

		if (icon) {
			sf::Image _icon;
			if(_icon.loadFromFile(icon))
				_cat_window->window->setIcon(_icon.getSize().x, _icon.getSize().y,_icon.getPixelsPtr());
		}
		_cat_window->window->setKeyRepeatEnabled(false);

		return true;
	}
	return false;
}

void cat_resize_window(cat_window_t* _cat_window, int width, int height)
{
	cat_data_t* config = cat_load_default_config();
	if (!config)
		return;

	int WIDTH, HEIGHT,reWidth,reHeight;
	WIDTH = config->json["decoration"]["window_size"][0].asInt();
	HEIGHT = config->json["decoration"]["window_size"][1].asInt();

	if (WIDTH != width&& WIDTH>0) {
		reWidth = width;
		reHeight = width * HEIGHT / WIDTH;
	}
	else if (HEIGHT != height&& HEIGHT>0)
	{
		reWidth = height * WIDTH / HEIGHT;
		reHeight = height;
	}
	else{
		reWidth = WIDTH;
		reHeight = HEIGHT;
	}
	
	_cat_window->window->setSize(sf::Vector2u(reWidth, reHeight));
	
	if (reWidth > 0)
		config->json["main_decoration"]["window_size"][0] = reWidth;
	if (reHeight >0)
		config->json["main_decoration"]["window_size"][1] = reHeight;	
}

bool cat_display_window(cat_window_t* _cat_window)
{
	if (_cat_window && _cat_window->window && _cat_window->window->isOpen()) {
		_cat_window->window->display();
		return true;
	}
	return false;
}

void cat_clear_window(cat_window_t* _cat_window)
{
	cat_data_t* config = cat_load_default_config();	

	_cat_window->window->clear(
			sf::Color(
				config->json["main_decoration"]["rgb"][0].asInt(),
				config->json["main_decoration"]["rgb"][1].asInt(),
				config->json["main_decoration"]["rgb"][2].asInt(),
				config->json["main_decoration"]["rgb"][3].asInt()
			)
		);
}

void cat_updata_window(cat_window_t* _cat_window)
{
	cat_data_t* config = cat_load_default_config();
	_cat_window->window->setTopMost(config->json["main_decoration"]["topWindow"].asBool());
	_cat_window->window->setFramerateLimit(config->json["main_decoration"]["framerateLimit"].asInt());
}

void cat_close_window(cat_window_t* _cat_window)
{
	if (_cat_window && _cat_window->window && _cat_window->window->isOpen())
		_cat_window->window->close();
}

sf::WindowHandle cat_get_window_handle(cat_window_t* _cat_window)
{
	return _cat_window->window->getSystemHandle();
}

cat_window_t* cat_creat_window()
{
	struct cat_window* data = (cat_window*)bzalloc(sizeof(struct cat_window));
	data->window = new sf::RenderWindow();
	data->catfont = new sf::Font();

	return data;
}

bool cat_set_font(cat_window_t* _cat_window,const char* filepath)
{
	if (!filepath)
		return false;

	return _cat_window->catfont->loadFromFile(filepath);
}

static inline void cat_window_destory(cat_window_t* _cat_window) {
	delete _cat_window->window;
	_cat_window->catfont;	
	bfree(_cat_window);
}

void cat_release_window(cat_window_t* _cat_window)
{
	if (!_cat_window)
		return;

	cat_window_destory(_cat_window);
}

